//
//  newsModel.m
//  网易新闻
//
//  Created by mac on 16/7/21.
//  Copyright © 2016年 xbk. All rights reserved.
//

#import "newsModel.h"
#import "download.h"

@implementation newsModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

+ (instancetype)downloadWithDic:(NSDictionary *)dic{

    newsModel *model = [[newsModel alloc]init];
    
    [model setValuesForKeysWithDictionary:dic];
    
    return model;
}

+ (void) downloadWithUrl:(NSString *)url successBlock:(void(^)(NSArray *arr))successBlock failBlock:(void(^)(NSError *error))failBlock{

    [[download sharedownload]GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
        //NSLog(@"%@",responseObject);
        NSDictionary *dic = responseObject;
        
        //动态的获取字典的key.原因:不同的新闻,key不一样.
        NSString *key = responseObject.keyEnumerator.nextObject;
        
        //通过key获取字典内的数组
        NSArray *arr = dic[key];
        
        NSMutableArray *arrM = [NSMutableArray array];
        
        for (NSDictionary *dic in arr) {
        
            newsModel *model = [newsModel downloadWithDic:dic];
            
            [arrM addObject:model];
        }
        
        if (successBlock) {
            successBlock(arrM.copy);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
}

@end
